<template>
  <a-modal
    :open="open"
    width="780px"
    :title="rowData?.id ? '修改实况信息' : '新增实况信息'"
    centered
    :ok-button-props="{ disabled: !isCompleted }"
    ok-text="保存"
    @ok="onOk"
    @cancel="onCancel"
  >
    <a-form :model="formState" class="form-style-flex" :label-col="{ style: { width: '80px' } }">
      <a-form-item label="区域名称" class="w-full">
        <a-input v-model:value="formState.areaName" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="经纬度" class="w-1/2">
        <a-input v-model:value="formState.location" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="气温" class="w-1/2">
        <a-input v-model:value="formState.temperature" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="风力" class="w-1/2">
        <a-input v-model:value="formState.windPower" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="风向" class="w-1/2">
        <a-input v-model:value="formState.windDirection" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="天气" class="w-1/2">
        <a-input v-model:value="formState.weather" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="海况" class="w-1/2">
        <a-input v-model:value="formState.seaState" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="浪高" class="w-1/2">
        <a-input v-model:value="formState.waveHeight" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="水温" class="w-1/2">
        <a-input v-model:value="formState.waterTemperature" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="高潮" class="w-1/2">
        <a-input v-model:value="formState.highTide" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="高潮时间" class="w-1/2">
        <a-date-picker v-model:value="formState.highTideTime" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
      </a-form-item>
      <a-form-item label="低潮" class="w-1/2">
        <a-input v-model:value="formState.lowTide" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="低潮时间" class="w-1/2">
        <a-date-picker v-model:value="formState.lowTideTime" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'

const emit = defineEmits(['update:open', 'ok'])
const props = defineProps({
  open: {
    type: Boolean,
    default: false
  },
  rowData: {
    type: Object,
    default: () => ({})
  }
})
const formState = ref({
  areaName: '',
  location: '',
  temperature: '',
  windPower: '',
  windDirection: '',
  weather: '',
  seaState: '',
  waveHeight: '',
  waterTemperature: '',
  highTide: '',
  highTideTime: '',
  lowTide: '',
  lowTideTime: ''
})
const rulesState = ref({})
const { isCompleted, resetFields, validate } = useForm(formState, rulesState)

watch(
  () => props.open,
  (val) => {
    if (val) {
      if (props.rowData?.id) {
        formState.value = props.rowData
      } else {
        formState.value.meetingTime = dayjs().format('YYYY-MM-DD')
      }
    }
  }
)

const onCancel = () => {
  emit('update:open', false)
  resetFields()
  formState.value = {
    areaName: '',
    location: '',
    temperature: '',
    windPower: '',
    windDirection: '',
    weather: '',
    seaState: '',
    waveHeight: '',
    waterTemperature: '',
    highTide: '',
    highTideTime: '',
    lowTide: '',
    lowTideTime: ''
  }
}
const onOk = async () => {
  await validate()
  const { code, msg } = await post('/war/warWeatherRealtime/doSaveWarWeatherRealtime', {
    ...formState.value
  })
  if (code === 0) {
    message.success('保存成功')
    onCancel()
    emit('ok')
  } else {
    message.error(msg)
  }
}
</script>
